package com.example.tianling.common.utils;

import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduledTask {
    private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    public void startScheduledTaskByMinute(int minutes, Runnable task) {
        executorService.schedule(task, minutes, TimeUnit.MINUTES);
    }
    public void startScheduledTaskBySecond(int seconds, Runnable task) {
        executorService.schedule(task, seconds, TimeUnit.SECONDS);
    }
    public void startScheduledTaskByHour(int hours, Runnable task) {
        executorService.schedule(task, hours, TimeUnit.HOURS);
    }

    public void CyclicScheduledTaskBySecond(int second, Runnable task) {
        executorService.scheduleAtFixedRate(task, 10, second, TimeUnit.SECONDS);
    }
}
